//
//  CVPageControl.m
//  eConsume
//
//  Created by haoyi on 14-3-5.
//  Copyright (c) 2014年 Hooy1. All rights reserved.
//

#import "CVPageControl.h"

@interface
CVPageControl(private) // 声明一个私有方法, 该方法不允许对象直接使用

-(void)updateDots;

@end

@implementation CVPageControl


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _imagePageStateNormal = [UIImage imageNamed:@"img_pagecontrol.png"];
        _imagePageStateHighlighted = [UIImage imageNamed:@"img_pagecontrol_f.png"];
    }
    return self;
}

-(void)setCurrentPage:(NSInteger)page{
    
    [super setCurrentPage:page];
    [self updateDots];
}

-(void)updateDots{ // 更新显示所有的点按钮
    
    for (int i = 0; i < [self.subviews count]; i++){
        
        UIView *dotView = [self.subviews objectAtIndex:i];
        UIImageView *dot = nil;
        
        for (UIView* subview in dotView.subviews){
            
            if ([subview isKindOfClass:[UIImageView class]]){
                
                dot = (UIImageView *)subview;
                
                break;
            }
        }
        
        if (dot == nil){
            
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                                dotView.frame.size.width,
                                                                dotView.frame.size.height)];
            [dotView addSubview:dot];
        }
        
        if (i == self.currentPage){
            
            if(_imagePageStateNormal)
                dot.image = _imagePageStateNormal;
        }
        else{
            
            if (_imagePageStateHighlighted)
                dot.image = _imagePageStateHighlighted;
        }
    }
}

@end
